{createAction} = require('redux-actions')

actions =
  getListStart:createAction('PLANDETAIL/GET_LIST_START')
  getListCompleted:createAction('PLANDETAIL/GET_LIST_COMPLETED')
  refreshListStart:createAction('PLANDETAIL/REFRESH_LIST_START')
  refreshListCompleted:createAction('PLANDETAIL/REFRESH_LIST_COMPLETED')
  fetchGetList:(id)->
    (dispatch)->
      debugger
      dispatch actions.getListStart()
      fetch("http://202.136.214.173/api/V_SHORT_INPLAN?id=#{id}",{
          method: 'GET'
          mode: 'cors'
          cache: 'default'
          headers: { 'Content-Type':'application/x-www-form-urlencoded'}
          # body:"pageNum=#{pageNum}&state=1"
        })
      .then (res)->
        res.json()
      .then (json)->
        date = new Date()
        dispatch actions.getListCompleted
          model:json
          lastFetchTime:date
        dispatch type:'PLANPILOT/REFRESH_LIST_START'
        fetch("http://202.136.214.173/api/PilotList?planId=#{id}",{
            method: 'GET'
            mode: 'cors'
            cache: 'default'
            headers: { 'Content-Type':'application/x-www-form-urlencoded'}
            # body:"pageNum=#{pageNum}&state=1"
          })
      .then (res)->
        res.json()
      .then (json)->
        dispatch
          type:'PLANPILOT/REFRESH_LIST_COMPLETED'
          payload:
            list:json
            key:id
      .catch (err)->
        console.log(err)
  fetchRefreshList:(id)->
    (dispatch)->
      dispatch actions.refreshListStart()
      fetch("http://202.136.214.173/api/V_SHORT_INPLAN?id=#{id}",{
          method: 'GET'
          mode: 'cors'
          cache: 'default'
          headers: { 'Content-Type':'application/x-www-form-urlencoded'}
          # body:"pageNum=#{pageNum}&state=1"
        })
      .then (res)->
        res.json()
      .then (json)->
        debugger
        date = new Date()
        dispatch actions.refreshListCompleted
          model:json
          lastFetchTime:date
        dispatch type:'PLANPILOT/GET_LIST_START'
        fetch("http://202.136.214.173/api/PilotList?planId=#{id}",{
            method: 'GET'
            mode: 'cors'
            cache: 'default'
            headers: { 'Content-Type':'application/x-www-form-urlencoded'}
            # body:"pageNum=#{pageNum}&state=1"
          })
      .then (res)->
        res.json()
      .then (json)->
        dispatch
          type:'PLANPILOT/GET_LIST_COMPLETED'
          payload:
            list:json
            key:id
      .catch (err)->
        console.log(err)

module.exports = actions
